package com.utils.commons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateHelper {
	
	private final static Logger logger = LoggerFactory.getLogger(DateHelper.class);
	
	/**
	 * 取整点
	 * @param date
	 * @return
	 */
	public static Date dateFormat_yyyy_MM_dd_HH_00_00(Date date){
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获取昨天每小时时间点
	 * @param date
	 * @return
	 */
	public static List<Date> getYestodayEveryHours(Date date){
		List<Date> list = new ArrayList<Date>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getTodaysBegin(date));
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
		for (int i = 0; i < 24; i++) {
			list.add(calendar.getTime());
			calendar.add(Calendar.HOUR_OF_DAY, 1);
		}
        return list;
	}
	
	/**
	 * 当天凌晨
	 * @param date 当天
	 * @return
	 */
	public static Date getTodaysBegin(Date date) {
		date = dateFormat_yyyy_MM_dd_HH_00_00(date);
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		return calendar.getTime();
	}
	
	/**
	 * 加小时
	 * @param date
	 * @param x 小时数
	 * @return
	 */
	public static Date addHours(Date date, int x){
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, x);
        return calendar.getTime();
	}
	
	/**
	 * 之前或之后    N（年，月，日）
	 * @param date
	 * @param year 例子：-1 = 去年
	 * @param month
	 * @param day
	 * @param minute
	 * @return
	 */
	public static Date beforeOrAfterTime(Date date, int year, int month, int day, int minute){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);// 设置时间
		calendar.add(Calendar.YEAR, year); // 年份  
		calendar.add(Calendar.MONTH, month);// 月份  
		calendar.add(Calendar.DATE, day);// 日期  
		calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
	}
	
	/**
	 * 是不是在n分钟之内
	 * @param date
	 * @return
	 */
	public static boolean isInTime_min(Date date, int i){
		if(date.before(beforeOrAfterTime(new Date(),0,0,0,-i))){
			return false;
		}
		if(date.after(beforeOrAfterTime(new Date(),0,0,0,i))){
			return false;
		}
		return true;
	}
	
	public static String DateFormatToString(String format, Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	/**
	 * 如果转换失败会返回当前时间
	 * @param format
	 * @param date
	 * @return
	 */
	public static Date DateFormatToDate(String format, String date){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date d = new Date();
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			logger.error("时间转换异常:<"+date+">不能转换为<"+format+">,直接返回当前时间");
		}
		return d; 
	}
	/***
	 * 解析格式为yyyy-MM-dd HH:mm:ss 的字符串成date格式
	 * @param date
	 * @return
	 */
	public static Date StringToDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date d=null;
		try {
			d = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d; 
	}
}
